function asyncAdd(a, b, cb) {
  setTimeout(() => {
    cb(a + b);
  }, Math.random() * 1000);
}

async function sum(...args) {
  if (args.length % 2 !== 0) {
    args.push(0);
  }

  const promises = [];
  const mid = args.length / 2; // 折半
  for (let i = 0; i < mid; i++) {
    promises.push(
      new Promise((resolve) => {
        asyncAdd(args[i], args[i + mid], resolve);
      })
    );
  }

  const rest = await Promise.all(promises);
  let res;
  if (rest.length === 0) res = 0;
  else if (rest.length === 1) res = rest[0];
  else res = sum(...rest); // 递归
  return Promise.resolve(res);
}

sum(1, 2, 3).then((res) => {
  console.log(res);
}); // 6
